package com.sh4.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.maven.shared.utils.StringUtils;
import org.springframework.stereotype.Repository;

import com.sh4.dao.ArticleDao;
import com.sh4.entity.Article;

import net.sf.json.JSONObject;

@Repository
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements ArticleDao{

	@Override
	public List<Map<String, Object>> list(int pageSize, int pageNo, JSONObject params) {
		
		String type = params.getString("type");
		String title = params.getString("title");
		String desc = params.getString("desc");
		
		String sql = "";
		List<String> values = new ArrayList<String>();
		
		if(pageNo == 0 && pageSize == 0) {//表示 查询所有 的 模糊查询
			sql = "select a.ID_ as id ,"
					+ "a.TYPE_ as type ,"
					+ "a.SECOND_TYPE_ as secondType ,"
					+ "a.TITLE_ as title , "
					+ "a.IMAGE_URL_ as imageUrl ,"
					+ "a.DESC_ as description , "
					+ "a.CONTENT_ as content ,"
					+ "a.CREATE_BY_ as createBy ,"
					+ "DATE_FORMAT(a.CREATE_DATE_ ,'%Y-%m-%d') as createDate,"
					+ "a.UPDATE_BY_ as updateBy , "
					+ "a.UPDATE_DATE_ as updateDate   "
					+ "from article a where a.TITLE_ like ? or a.DESC_ like ? or a.CONTENT_ like ? order by a.CREATE_DATE_ desc";
			
			return this.getArrayBySql(sql, "%"+title+"%","%"+title+"%","%"+title+"%");
		}
		
		if(StringUtils.isEmpty(type)) {
			
			sql = "select a.ID_ as id ,"
					+ "a.TYPE_ as type ,"
					+ "a.SECOND_TYPE_ as secondType ,"
					+ "a.TITLE_ as title , "
					+ "a.IMAGE_URL_ as imageUrl ,"
					+ "a.DESC_ as description , "
					+ "a.CONTENT_ as content ,"
					+ "a.CREATE_BY_ as createBy ,"
					+ "DATE_FORMAT(a.CREATE_DATE_ ,'%Y-%m-%d') as createDate,"
					+ "a.UPDATE_BY_ as updateBy , "
					+ "a.UPDATE_DATE_ as updateDate   "
					+ "from article a where a.TITLE_ like ? and a.DESC_ like ? order by a.CREATE_DATE_ desc";
			
			
			return this.getArrayPageBySql(pageNo, pageSize, sql , "%"+title+"%", "%"+desc+"%");
		}
		
		if(params.containsKey("secondType") && StringUtils.isNotEmpty(params.getString("secondType"))) {
			
			sql = "select a.ID_ as id ,"
					+ "a.TYPE_ as type ,"
					+ "a.SECOND_TYPE_ as secondType ,"
					+ "a.TITLE_ as title , "
					+ "a.DESC_ as description , "
					+ "a.CONTENT_ as content ,"
					+ "a.IMAGE_URL_ as imageUrl ,"
					+ "a.CREATE_BY_ as createBy ,"
					+ "DATE_FORMAT(a.CREATE_DATE_ ,'%Y-%m-%d') as createDate,"
					+ "a.UPDATE_BY_ as updateBy , "
					+ "a.UPDATE_DATE_ as updateDate  "
					+ "from article a where 1=1 ";
			
			if(params.containsKey("type") && StringUtils.isNotEmpty(params.getString("type"))) {
				
				String type1 = params.getString("type");
				sql += " and a.TYPE_ = ?";
				values.add(type1);
			}
			
			if(params.containsKey("secondType") && StringUtils.isNotEmpty(params.getString("secondType"))) {
				
				String secondType = params.getString("secondType");
				sql += " and a.SECOND_TYPE_ = ?";
				values.add(secondType);
			}
			
			if(params.containsKey("title") && StringUtils.isNotEmpty(params.getString("title"))) {
				String title1 = params.getString("title");
				sql += " and a.TITLE_ like ?";
				values.add(title1);
			}
			
			if(params.containsKey("desc") && StringUtils.isNotEmpty(params.getString("desc"))) {
				String desc1 = params.getString("desc");
				sql += " and a.DESC_ like ?";
				values.add(desc1);
			}
			
			sql += " order by a.CREATE_DATE_ desc";
			
			return this.getArrayPageBySql(pageNo, pageSize, sql, params.getString("type"),params.getString("secondType"));
		}
		
		sql = "select a.ID_ as id ,"
				+ "a.TYPE_ as type ,"
				+ "a.SECOND_TYPE_ as secondType ,"
				+ "a.TITLE_ as title , "
				+ "a.DESC_ as description , "
				+ "a.CONTENT_ as content ,"
				+ "a.IMAGE_URL_ as imageUrl ,"
				+ "a.CREATE_BY_ as createBy ,"
				+ "DATE_FORMAT(a.CREATE_DATE_ ,'%Y-%m-%d') as createDate,"
				+ "a.UPDATE_BY_ as updateBy , "
				+ "a.UPDATE_DATE_ as updateDate  "
				+ "from article a where a.TYPE_ = ? and a.TITLE_ like ? and a.DESC_ like ?  order by a.CREATE_DATE_ desc";
		
		return this.getArrayPageBySql(pageNo, pageSize, sql, type , "%"+title+"%", "%"+desc+"%");
	}

	@Override
	public Article getArticleById(String id , String type) {
		
		String hql = "from Article a where a.id = ? and a.type = ?";
		
		return this.getByHql(hql, id,type);
	}

	@Override
	public Map<String, Object> preArticle(Date createDate , String type) {
		
		String sql = "select a.ID_ as id , a.TITLE_ as title , a.DESC_ as description , a.IMAGE_URL_ as imageUrl from article a where a.TYPE_ = ? and a.CREATE_DATE_ < ? order by a.CREATE_DATE_ desc limit 1 ";
		
		return this.getUniqueResultBySql(sql, type, createDate);
	}
	@Override
	public Map<String, Object> suffixArticle(Date createDate , String type) {
		String sql = "select a.ID_ as id , a.TITLE_ as title , a.DESC_ as description , a.IMAGE_URL_ as imageUrl  from article a where a.TYPE_ = ? and a.CREATE_DATE_ > ? order by a.CREATE_DATE_ asc limit 1 " ;
		return this.getUniqueResultBySql(sql, type,createDate);
	}

	@Override
	public int countArticle(JSONObject params , int pageSize, int pageNo) {
		
		String type = params.getString("type");
		String title = params.getString("title");
		String desc = params.getString("desc");
		
		String sql = "";
		
		List<String> values = new ArrayList<String>();
		
		if(pageNo == 0 && pageSize == 0) {//表示 查询所有 的 模糊查询
			sql = "select count(0) as total"
					+ "from article a where a.TITLE_ like ? or a.DESC_ like ? or a.CONTENT_ like ? ";
			
			BigInteger count = (BigInteger)this.getUniqueResultBySql(sql, "%"+title+"%","%"+title+"%","%"+title+"%").get("total");
			
			return count.intValue();
		}
		
		sql = "select count(0) as total "
				+ "from article a where 1=1 ";
		
		if(StringUtils.isNotEmpty(type)) {
			
			if(params.containsKey("type") && StringUtils.isNotEmpty(params.getString("type"))) {
				
				String type1 = params.getString("type");
				sql += " and a.TYPE_ = ?";
				values.add(type1);
			}
			
			if(params.containsKey("secondType") && StringUtils.isNotEmpty(params.getString("secondType"))) {
				
				String secondType = params.getString("secondType");
				sql += " and a.SECOND_TYPE_ = ?";
				values.add(secondType);
			}
			
			if(params.containsKey("title") && StringUtils.isNotEmpty(params.getString("title"))) {
				String title1 = params.getString("title");
				sql += " and a.TITLE_ like ?";
				values.add(title1);
			}
			
			if(params.containsKey("desc") && StringUtils.isNotEmpty(params.getString("desc"))) {
				String desc1 = params.getString("desc");
				sql += " and a.DESC_ like ?";
				values.add(desc1);
			}
			
		}
		
		
		BigInteger count = (BigInteger)this.getUniqueResultBySql(sql , values.toArray(new String [] {})).get("total");
		
		return count.intValue();
	}

}
